import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:lib_common/components/widget/ntt_auto_size_text.dart';
import 'package:lib_common/components/widget/ntt_image_view.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_common/components/widget/ntt_ordinary_button.dart';

import '../../r.dart';

/// 错误状态视图
class NttErrorStatus extends StatelessWidget {
  /// 错误状态类型
  final NttErrorStatusType statusType;

  /// 描述文本
  final String message;

  /// 描述文本样式
  final TextStyle? messageStyle;

  /// 按钮文本 按钮文本不为空时会显示按钮
  final String? buttonText;

  /// 按钮点击事件
  final Function()? onButtonClick;

  /// 按钮文本颜色
  final Color? buttonTextColor;

  /// 如果默认样式不满足需求 可以自己传入一个
  final Widget? buttonWidget;

  /// 如果内置的图片不满足需要自定义图片时使用
  final String? imagePath;

  /// 图片宽高 默认是 320 * 240
  final double? imageWidth;
  final double? imageHeight;

  const NttErrorStatus({
    super.key,
    required this.statusType,
    this.message = "出错啦~",
    this.buttonText,
    this.messageStyle,
    this.imagePath,
    this.imageWidth,
    this.imageHeight,
    this.onButtonClick,
    this.buttonWidget,
    this.buttonTextColor,
  });

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          NttImageView(
            imageUrl: imagePath ?? _imageUrl,
            width: imageWidth ?? 320.w,
            height: imageHeight ?? 240.h,
            fit: BoxFit.contain,
          ),
          SizedBox(
            height: 32.h,
          ),
          NttAutoSizeText(
            message,
            style: messageStyle,
            fontSize: 36.r,
            minFontSize: 30.r,
            maxFontSize: 50.r,
            maxLines: 3,
            overflow: TextOverflow.ellipsis,
          ),
          SizedBox(
            height: 64.h,
          ),
          if (buttonWidget != null) buttonWidget!,
          if (buttonWidget == null && buttonText != null)
            Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                NttOrdinaryButton(
                  buttonText ?? "重试",
                  textColor: buttonTextColor ?? Colors.white,
                  textSize: 40.r,
                  width: 100.w,
                  height: 88.h,
                  radius: 50.r,
                  // maxWidth: 200.w,
                  padding: EdgeInsets.symmetric(horizontal: 40.w),
                  onTap: () {
                    onButtonClick?.call();
                  },
                )
              ],
            )
        ],
      ),
    );
  }

  /// 获取错误状态图片
  String get _imageUrl {
    switch (statusType) {
      case NttErrorStatusType.error:
        return R.icon_toast_1_png;
      case NttErrorStatusType.loading:
        return R.icon_toast_1_png;
      case NttErrorStatusType.empty:
        return R.icon_toast_1_png;
      case NttErrorStatusType.defaultStatus:
        return R.icon_toast_1_png;
    }
  }
}

/// 错误状态类型
enum NttErrorStatusType {
  /// 错误
  error,

  /// 加载中
  loading,

  /// 空
  empty,

  /// 默认
  defaultStatus
}
